/*
 * Copyright 2013 Walter Lütgenau.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package offset.nodes.client.vdialog.navigation.view;

import offset.nodes.client.vdialog.view.navigation.VirtualPagePanel;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.jcr.PropertyType;
import javax.swing.JButton;
import javax.swing.JPanel;
import offset.nodes.client.dialog.navigation.view.ElementInfo;
import offset.nodes.client.dialog.navigation.view.NavigationApplet;
import offset.nodes.client.dialog.navigation.view.NodeInfo;
import offset.nodes.client.dialog.navigation.view.PropertyInfo;
import offset.nodes.client.vdialog.model.navigation.VirtualPageModel;
import offset.nodes.client.model.Server;
import offset.nodes.client.model.ServerModel;

/**
 *
 * @author Walter Lütgenau
 */
public class DebugVirtualPagePanel extends NavigationApplet {

    public void init() {
        ServerModel.setUserName("admin");
        ServerModel.setPassword("nodes");

        super.init();
    }

    public void initUI() {
        super.initUI();

        DebugVirtualPageModel headingModel = new DebugVirtualPageModel(this, new VirtualPagePanel(), getServer());
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(headingModel.getPanel(), BorderLayout.CENTER);

        JButton updateButton = new JButton("Update");
        updateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                update();
            }
        });

        panel.add(updateButton, BorderLayout.SOUTH);

        models.put(headingModel.getPanel().getCardName(), headingModel);
        navigationPanel.add(panel, headingModel.getPanel().getCardName());

        navigationPanel.showCard(headingModel.getPanel().getCardName());
    }

    class DebugVirtualPageModel extends VirtualPageModel {
        String useCase = "nodes:useCase";
        String useCaseActor = "nodes:useCaseActor";
        String property = "property";
        
        class ParserInfo {
            Object parent;
            String query;
            ElementInfo element;

            public ParserInfo(Object parent, String query, ElementInfo element) {
                this.parent = parent;
                this.query = query;
                this.element = element;
            }

            public Object getParent() {
                return parent;
            }

            public ElementInfo getElement() {
                return element;
            }

            public String getQuery() {
                return query;
            }
        }
        
        ParserInfo[][] elements = new ParserInfo[][] {
            {
                new ParserInfo(null, "", new NodeInfo(useCase, "nodes:useCase", "useCase1")),
                new ParserInfo(useCase, "", new PropertyInfo("nodes:useCase/@NAME", 1, "NAME1")),
                new ParserInfo(useCase, "", new PropertyInfo("nodes:useCase/@nodes:id", 9, "id")),
                new ParserInfo(useCase, "secondary1", new NodeInfo(useCaseActor, "nodes:useCaseActor", "useActor1")),
                new ParserInfo(useCaseActor, "secondary1", new PropertyInfo("nodes:useCaseActor/@NAME", 1, "NAME1")),
                new ParserInfo(useCase, "secondary1", new NodeInfo(useCaseActor, "nodes:useCaseActor", "useActor1")),
                new ParserInfo(useCaseActor, "secondary1", new PropertyInfo("nodes:useCaseActor/@NAME", 1, "NAME1")),
                new ParserInfo(useCase, "", new NodeInfo("nodes:useCase/nodes:description", "nodes:htmlDiv", "description1"))
            },
            {
                new ParserInfo(null, "", new NodeInfo(useCase, "nodes:useCase", "useCase")),
                new ParserInfo(useCase, "", new PropertyInfo("nodes:useCase/@NAME", 1, "NAME")),
                new ParserInfo(useCase, "", new PropertyInfo("nodes:useCase/@nodes:id", 9, "id")),
                new ParserInfo(useCase, "secondary1", new NodeInfo(useCaseActor, "nodes:useCaseActor", "useActor")),
                new ParserInfo(useCaseActor, "secondary1", new PropertyInfo("nodes:useCaseActor/@NAME", 1, "NAME")),
                new ParserInfo(useCase, "secondary1", new NodeInfo(useCaseActor, "nodes:useCaseActor", "useActor")),
                new ParserInfo(useCaseActor, "secondary1", new PropertyInfo("nodes:useCaseActor/@NAME", 1, "NAME")),
                new ParserInfo(useCase, "", new NodeInfo("nodes:useCase/nodes:description", "nodes:htmlDiv", "description"))
            }
        };
        int page = 0;

        public DebugVirtualPageModel(Applet applet, VirtualPagePanel panel, Server server) {
            setApplet(applet);
            initPanel();
            setServer(server);
        }

        public void updatePage() throws Exception {
            panel.start();
            
            for (ParserInfo parserInfo : elements[page]) {
                if (parserInfo.getElement() instanceof NodeInfo) {
                    NodeInfo nodeInfo = (NodeInfo) parserInfo.getElement();
                    AssociationInfo associationInfo = parseDataAttribute((String) nodeInfo.getUserObject());
                    panel.addNode(parserInfo.getParent(), nodeInfo.getUserObject(), parserInfo.getQuery(), associationInfo.getNodeType(), nodeInfo.getName());
                } else {
                    PropertyInfo propertyInfo = (PropertyInfo) parserInfo.getElement();
                    int propertyType = -1;
                    if (((String) propertyInfo.getUserObject()).endsWith("NAME"))
                        propertyType = PropertyType.STRING;
                    else
                        propertyType = parseDataAttribute((String) propertyInfo.getUserObject()).getPropertyType();
                    panel.addProperty(parserInfo.getParent(), propertyInfo.getUserObject(), parserInfo.getQuery(), propertyType, propertyInfo.getName());
                }
            }
            page++;
            
            panel.end();
        }

        public void nodeSelected(String location) {
            System.out.println(location);
        }
    }

    public static void main(String[] arguments) {
        sun.applet.AppletViewer.main(new String[]{"src/test/java/offset/nodes/client/dialog/navigation/view/DebugVirtualPagePanel.html"});
    }
}
